!function (window,document,$,undefined) {

    var $dlg = $("#goodsDlg");

    /*程序的唯一入口*/
    var init = function () {
        
        initEvent();
        getTableData();

    };

    /*渲染列表*/
    var getTableData = function () {

        var url = "api/shopping_goods_list.php";

        setTimeout(function () {
            layer.msg('商品加载中', {icon: 16, time:0, shade: [0.2, '#000']});
        },0);
        $.get(url,function (response) {
            if(response.success) {
                renderTable(response);
                layer.closeAll();
            } else {

            }
        },"json");

    };

    var renderTable = function (response) {
        var data = response.data,
            trArr = [];

        $.each(data, function (i,obj) {
            trArr.push(
                '<tr>',
                    '<td><input id="',obj.id,'" type="checkbox"></td>',
                    '<td>',i+1,'</td>',
                    '<td>',obj.title,'</td>',
                    '<td>￥',obj.price,'</td>',
                    '<td>',obj.details,'</td>',
                    '<td>',obj.amount,'</td>',
                    '<td>',obj.classify,'</td>',
                '</tr>'
            );
        });
        $("#goodsTable tbody").html(trArr.join(''));
 
    };

    /*绑定事件入口*/
    var initEvent = function () {
        $("#newBtn").on("click",onNewBtnClick);
        $("#saveBtn").on("click",onSaveBtnClick);
        $("#delBtn").on("click",onDelBtnClick);
        $("#updaBtn").on("click",onUpdaBtnClick);
        $("#goodsTable").on("change", "tbody input[type=checkbox]",onChkBoxClick);
    };

    var onUpdaBtnClick = function() {
        alert(99)
    };

    /*复选按钮点击事件*/
    var onChkBoxClick = function () {
        var $chkbox = $("#goodsTable tbody input[type=checkbox]:checked");
        var $delBtn = $("#delBtn");
        var $updaBtn = $("#updaBtn");
        if ($chkbox.length > 0) {  
            $delBtn.removeProp("disabled");
        } else {
            $delBtn.prop("disabled","disabled");
            $updaBtn.prop("disabled","disabled");
        }

        if ($chkbox.length == 1) {
            $updaBtn.removeProp("disabled");
        } else {
            $updaBtn.prop("disabled","disabled");
        }
    };

    /*删除按钮点击事件*/
    var onDelBtnClick = function () {
        var $chkbox = $("#goodsTable tbody input[type=checkbox]:checked");
        var rArr = [];
        var url = "api/shopping_goods_del.php";

        layer.confirm('确定要删除所选商品？', {
          btn: ['取消','确定'] //按钮
        }, function(){
            layer.closeAll();
        }, function(){

        layer.msg('商品加载中', {icon: 16, time:0, shade: [0.2, '#000']});
        $chkbox.each(function () {
            rArr.push(this.id);
        });
        
        $.get(url,{ids:rArr.join(",")},function (response) {
            if (response.success) {
                layer.msg('商品删除成功！', {offset: 0,});
                getTableData();
                $("#delBtn").prop("disabled","disabled");
            } else {

            }
        },"json");
        });
        
    };


    /*表单内保存按钮点击事件*/
    var onSaveBtnClick = function () {
        var url = "api/shopping_goods_add.php";
        var data = {
            title:$("#title").val(),
            price:$("#price").val(),
            details:$("#detail").val(),
            amount:$("#amount").val(),
            classify:$("#classify").val(),
            status:$("input[name=status]:checked").val(),
        };

        /*var index = layer.load(2, {
          shade: [0.1,'#fff'] //0.1透明度的白色背景
        });*/

        // 表单验证
        
        if (data.title == "") {
            alert("商品名称不能为空！");
            return;
        }

        if (data.price == "") {
            alert("商品价格不能为空！");
            return;
        }

        if (data.amount == "") {
            alert("商品库存不能为空！");
            return;
        }

        layer.msg('商品加载中', {icon: 16, time:0, shade: [0.2, '#000']});

        $.get(url,data,function (response) {
            if (response.success) {
                layer.closeAll();
                $dlg.modal("hide");
                $("#gForm").trigger("reset");
                layer.msg('商品添加成功！', {offset: 0,});
                getTableData();
            } else {
                alert("商品添加失败！");
            }
        },"json");
     
    };

    /*新增按钮点击事件*/
    var onNewBtnClick = function () {
       $dlg.modal();
    };


    $(document).ready(init);
}(window,document,jQuery);